package com.joyxy.spider.mapscript.operation;

/**
 * 地图转换操作
 * 操作数: 4
 * 参数: X/Y/Key
 */
public class MapChangeOperation extends BaseOperation {
    /** 切换点横坐标 */
    private int switchX;
    /** 切换点纵坐标 */
    private int switchY;
    /** 按键类型: 0-按向上键, 1-按R键(钥匙) */
    private int keyType;
    
    public MapChangeOperation(int switchX, int switchY, int keyType) {
        super(4, "地图转换");
        this.switchX = switchX;
        this.switchY = switchY;
        this.keyType = keyType;
    }
    
    /**
     * 从参数数组创建地图转换操作
     */
    public static MapChangeOperation fromParameters(int[] parameters) {
        if (parameters == null || parameters.length < 3) {
            throw new IllegalArgumentException("地图转换操作需要3个参数: X/Y/Key");
        }
        return new MapChangeOperation(parameters[0], parameters[1], parameters[2]);
    }
    
    @Override
    public void execute() {
        String keyDesc = getKeyDescription();
        System.out.println("执行地图转换: 在坐标(" + switchX + "," + switchY + ") " + keyDesc);
        // 这里应该调用实际的地图转换逻辑
    }
    
    @Override
    public boolean isValid() {
        return switchX >= 0 && switchY >= 0 && (keyType == 0 || keyType == 1);
    }
    
    @Override
    public String getDetailInfo() {
        return String.format("地图转换: 切换点(%d,%d) %s", 
                switchX, switchY, getKeyDescription());
    }
    
    /**
     * 获取按键类型描述
     */
    public String getKeyDescription() {
        switch (keyType) {
            case 0: return "按向上键";
            case 1: return "按R键(钥匙)";
            default: return "未知按键";
        }
    }
    
    /**
     * 检查是否使用钥匙
     */
    public boolean isUsingKey() {
        return keyType == 1;
    }
    
    /**
     * 检查是否使用方向键
     */
    public boolean isUsingDirectionKey() {
        return keyType == 0;
    }
    
    /**
     * 检查指定坐标是否为切换点
     */
    public boolean isSwitchPoint(int x, int y) {
        return x == switchX && y == switchY;
    }
    
    // Getter methods
    public int getSwitchX() { return switchX; }
    public int getSwitchY() { return switchY; }
    public int getKeyType() { return keyType; }
    
    @Override
    public String toString() {
        return String.format("MapChange{pos=(%d,%d), key=%s}", 
                switchX, switchY, getKeyDescription());
    }
} 